<?php

namespace App\Exports;

use App\Models\User\User;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
use Maatwebsite\Excel\Concerns\WithHeadings;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;

class UserExport extends DefaultValueBinder implements FromArray,WithCustomValueBinder
{

    private $conditions;

    public function __construct($conditions) {
        $this->conditions = $conditions;
    }


    public function array():array {

        $data = [
            // 设置表头信息
            ['序号','姓名','性别','身份证','手机','住址','序列号','激活时间'],
        ];

        $records = (new User())->getLst($this->conditions)->with(['getDriverInfo'])->get();

        foreach ($records as $record) {
            $temp = [
                $record->id,

                $record->getDriverInfo->name??'',
                $record->getDriverInfo->gender??'',
                $record->getDriverInfo->id_card??'',
                $record->phone,
                $record->getDriverInfo->address??'',
                $record->serial_number,
                $record->active_time,
            ];
            $data[] = $temp;
        }
        return $data;


    }
    public function bindValue(Cell $cell, $value) {
        if (strlen($value) > 10) {
            $cell->setValueExplicit($value, DataType::TYPE_STRING);
            return true;
        }
        return parent::bindValue($cell, $value);
    }
}
